package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.UserAddress;
import com.hyk.dto.R;
import com.hyk.service.UserAddressService;
import com.hyk.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@Api(tags = "钱包地址控制器")
@RequestMapping("/userAddress")
public class UserAddressController {

    @Resource
    private UserAddressService userAddressService;

    @GetMapping
    @ApiOperation("钱包地址列表")
    public R<Page<UserAddress>> list(@ApiIgnore Page<UserAddress> page,
                                     String userId){
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(userId)){
            wrapper.eq("user_id",userId);
        }
        wrapper.orderByDesc("created");
        return R.ok(userAddressService.page(page,wrapper));
    }

    @GetMapping("/getCoinAddress/{coidId}")
    public R<UserAddress> getCoinAddress(@PathVariable Long coidId){
        UserAddress userAddress = userAddressService.getOne(new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getUserId, UserUtils.getUser())
                .eq(UserAddress::getCoinId, coidId));
        return R.ok(userAddress);
    }

}
